#include <stdio.h>
#define SERVER_PORT 2345

void child_proc_fun(int sockfd);

void child_exit(int signo)
{
	pid_t pid;
	while (1) {
		pid = waitpid(-1, NULL, WNOHANG);
		if (pid <= 0)
			break;
	}
}

int main(int argc, char **argv)
{
	int listen_sockfd, conn_fd;
	struct sockaddr servaddr, cliaddr;
	struct sockaddr_in *st_in = (struct sockaddr_in *)&servaddr;
	socklen_t len;

	listen_sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (listen_sockfd == -1) {
		exit(1);
	}
	bzero(&servaddr, sizeof(struct sockaddr));
	st_in->sin_family = AF_INET;
	st_in->sin_port = htons(SERVER_PORT);
	st_in->sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(listen_sockfd, &servaddr, sizeof servaddr) < 0) {
		exit(1);
	}
	if (listen(listen_sockfd, LISTEN_MAX) < 0) {
		exit(1);
	}
	signal(SIGCHLD, child_exit);
	
	while (1) {
		len = sizeof(cliaddr);
		if ((conn_fd = accept(listen_sockfd, &cliaddr, &len)) < 0) {
			if (errno == EINTR) {
				continue;
			}
			exit(1);
		}
		else {
			child_pid = fork();
			if (child_pid == 0) {	//child
				close(listen_sockfd);
				child_proc_fun(conn_fd);
				exit(0);
			}
			else {	//parent
				close(conn_fd);
				continue;
			}
		}
	}
	return 0;
}

void child_proc_fun(int sockfd)
{
}
